package com.wwwarehouse.resource_center.adapter.rules;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.ResourceEvaluationBean;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Joseph on 2017/9/6.
 *         <p/>
 */

public class SearRulesDeskPagersAdapter
        extends RecyclerView.Adapter<SearRulesDeskPagersAdapter.ImportOrdersPagersHolder> {

    private Context mContext = null;
    private List<ResourceEvaluationBean.ListBean> mList = null;
    private int mHeight = 0;
    private OnItemClickListener mListener = null;

    public SearRulesDeskPagersAdapter(Context context, List<ResourceEvaluationBean.ListBean> list, int height) {
        this.mContext = context;
        this.mList = list;
        this.mHeight = height;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ImportOrdersPagersHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ImportOrdersPagersHolder(
                View.inflate(mContext, R.layout.adapter_rule_desk_rule_pagers_adapter, null));
    }

    @Override
    public void onBindViewHolder(ImportOrdersPagersHolder holder, final int position) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.rl_item.getLayoutParams();
        params.height = (mHeight - ConvertUtils.dip2px(mContext, 20)) / 3;
        holder.mName.setMaxLines(mList.get(position).getCategoryName()==null?2:1);
        holder.mName.setText(mList.get(position).getCategoryName()==null?mList.get(position).getItemName():mList.get(position).getCategoryName());
        double num = (double)mList.get(position).getRsEarning()/10000;
        BigDecimal b = new BigDecimal(num);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        String s1 = f1 + "";
        if (s1.substring(s1.indexOf(".")+1).length()==1){
            s1+="0";
        }
        holder.rsEarning.setText(s1);
        if (mList.get(position).getRsWeight().indexOf(".")==-1){
            if (mList.get(position).getRsWeight().startsWith("0")){
                holder.rsWeight.setText("资源权重"+mList.get(position).getRsWeight()+"%");
            }
        }else {
            String s = mList.get(position).getRsWeight().substring(2);
            if (s.startsWith("0")){
                s = s.substring(1);
            }
            holder.rsWeight.setText("资源权重"+s+"%");
        }
        int star = mList.get(position).getStar();
        for (int i = 0; i < star; i++) {
            (((ImageView)holder.llstart.getChildAt(i))).setImageResource(R.drawable.solidstar);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.ItemClickListener(position, v, mList.get(position).getCategoryUkid()==null?mList.get(position).getItemUkid():mList.get(position).getCategoryUkid());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface OnItemClickListener {
        void ItemClickListener(int position, View view, String data);
    }

    class ImportOrdersPagersHolder extends RecyclerView.ViewHolder {
        TextView mName;
        LinearLayout llstart ;
        TextView rsEarning;
        TextView rsWeight;
        RelativeLayout rl_item ;
        ImportOrdersPagersHolder(View itemView) {
            super(itemView);
            mName = (TextView) itemView.findViewById(R.id.title);
            llstart = (LinearLayout) itemView.findViewById(R.id.start);
            rsEarning = (TextView) itemView.findViewById(R.id.rs_earning);
            rsWeight = (TextView) itemView.findViewById(R.id.rs_weight);
            rl_item = (RelativeLayout) itemView.findViewById(R.id.rl_item);
        }
    }
}
